<?
/*
 * main php fo twitter center within htkou
 */
//define('IN_INDEX', TRUE);

session_start();
/* Load required lib files. */
require_once('JSON/JSON.php');
require_once('twitteroauth/twitteroauth.php');
require_once('config.php');

	/* If access tokens are not available redirect to signin page. */
	if (empty($_SESSION['access_token']) || empty($_SESSION['access_token']['oauth_token']) || empty($_SESSION['access_token']['oauth_token_secret'])) {
    	header('Location: ./cleartwitter.php');
	}

	//get user twitter mode
	if(isset($_REQUEST['mode'])) $select= $_REQUEST['mode'];
	else		$select= 'twitter';
	if(isset($_REQUEST['id'])) $append_tw_id= '&id='.$_REQUEST['id'];
	else 	$append_tw_id='';
	
	//handle signout and confirm first to get rid of Warning: Cannot modify header information - headers already sent by ... error
	if (($select =='signout') && (isset($_REQUEST['confirmed'])))
	{	
		//for signout
		//clearnup twitter session/cookie
		if (!(empty($_SESSION['access_token'])))
		{
			$access_token = $_SESSION['access_token'];
			/* Create a TwitterOauth object with consumer/user tokens. */
			$connection = new TwitterOAuth(CONSUMER_KEY, CONSUMER_SECRET, $access_token['oauth_token'], $access_token['oauth_token_secret']);
			// account/end_session
			$method = 'account/end_session';
			$response = $connection->POST($method);
		}
	    //clearnup session
		unset($_SESSION['twitter_logged']);
		unset($_SESSION['twitter_name']);
		unset($_SESSION['twitter_logged_image']);
		unset($_SESSION['twitter_id']);
  
		unset($_SESSION['oauth_token']);
		unset($_SESSION['oauth_token_secret']);
		unset($_SESSION['access_token']);

		/* Redirect to page with the connect to Twitter option. */
		header('Location: ./signin.php?force');
		exit;
	}
	
?>
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="zh-CN">
<head>
<title>twitter status</title>
<meta http-equiv="content-type" content="text/html; charset=UTF-8" />
<link rel="stylesheet" type="text/css"
	href="../twitter/style_tw.css" media="all" />
</head>
<body>

<div class="twitter_container"><?

/* If access tokens are not available redirect to connect page. */
if (!(empty($_SESSION['access_token'])))
{
//	echo    '<div id="header"><div class="description">';
//	echo " not empty access token or autohtoken, continue...";
	/* Get user access tokens out of the session. */
	$access_token = $_SESSION['access_token'];
	/* Create a TwitterOauth object with consumer/user tokens. */
	$connection = new TwitterOAuth(CONSUMER_KEY, CONSUMER_SECRET, $access_token['oauth_token'], $access_token['oauth_token_secret']);
	if(isset($_REQUEST['id']))
	{	//you are lookup anther user
		$id = intval($_REQUEST['id']);
		$method = 'users/show';
		$parameters = array('id' => $id);
		//$parameters = array('screen_name' => 'htk_firefox');
		$user = $connection->get($method, $parameters);
	}
	else //get your user info
	$user = $connection->get('account/verify_credentials');
	$screen_name = $user->screen_name;
	$user_name =  $user->name;
	$user_id =  $user->id;
//	echo "<p>user id is  {$user_id}";
//	echo "<p>screen name is  {$screen_name}";
//	echo "<p>User name is  {$user_name}";
//	echo    '</div></div>';

	echo	'<div id="header">';
	echo	'<img height="50" width="50" class="gravatar" src="'.$user->profile_image_url.'" alt=""/>';
	echo    '<h1><a href="http://twitter.com/'.$user->screen_name.'">'.$user->name.'('.$user->screen_name.')</a></h1>';
	echo    '<div class="description">'.$user->description.'</div>';
	echo    '<div id="tweet"><ul><li>'.$user->friends_count.' friends and '.$user->followers_count.' followers</li></ul></div>';
	echo	'</div>';


	echo 	'<div id="navigation">';
	echo 	'<ul>';
	if($select=='twitter')
	{
		echo    '<li><a class="selected" href="?mode=twitter'.$append_tw_id.'">Twitter</a></li>';
		echo     '<li><a href="?mode=friends'.$append_tw_id.'">Friends</a></li>';
		echo    '<li><a href="?mode=followers'.$append_tw_id.'">Followers</a></li>';
		if(isset($_REQUEST['id']))
		echo    '<li><a href="?mode=friendships'.$append_tw_id.'">Add To My Friend</a></li>';
		else
		echo    '<li><a href="?mode=signout">Sign out </a></li>';
	}
	else if ($select=='friends')
	{
		echo    '<li><a href="?mode=twitter'.$append_tw_id.'">Twitter</a></li>';
		echo     '<li><a class="selected" href="?mode=friends'.$append_tw_id.'">Friends</a></li>';
		echo    '<li><a href="?mode=followers'.$append_tw_id.'">Followers</a></li>';
		if(isset($_REQUEST['id']))
		echo    '<li><a href="?mode=friendships'.$append_tw_id.'">Add To My Friend</a></li>';
		else
		echo    '<li><a href="?mode=signout'.$append_tw_id.'">Sign out </a></li>';
	}
	else if ($select=='followers')
	{
		echo    '<li><a href="?mode=twitter'.$append_tw_id.'">Twitter</a></li>';
		echo     '<li><a href="?mode=friends'.$append_tw_id.'">Friends</a></li>';
		echo    '<li><a class="selected" href="?mode=followers'.$append_tw_id.'">Followers</a></li>';
		if(isset($_REQUEST['id']))
		echo    '<li><a href="?mode=friendships'.$append_tw_id.'">Add To My Friend</a></li>';
		else
		echo    '<li><a href="?mode=signout'.$append_tw_id.'">Sign out </a></li>';
	}
	else if ($select=='friendships')
	{
		echo    '<li><a href="?mode=twitter'.$append_tw_id.'">Twitter</a></li>';
		echo     '<li><a href="?mode=friends'.$append_tw_id.'">Friends</a></li>';
		echo    '<li><a  href="?mode=followers'.$append_tw_id.'">Followers</a></li>';
		if(isset($_REQUEST['id']))
		echo    '<li><a class="selected" href="?mode=friendships'.$append_tw_id.'">Add To My Friend</a></li>';
		else
		echo    '<li><a href="?mode=signout'.$append_tw_id.'">Sign out</a></li>';
	}
	else if ($select=='signout')
	{
		echo    '<li><a href="?mode=twitter'.$append_tw_id.'">Twitter</a></li>';
		echo     '<li><a href="?mode=friends'.$append_tw_id.'">Friends</a></li>';
		echo    '<li><a  href="?mode=followers'.$append_tw_id.'">Followers</a></li>';
		if(isset($_REQUEST['id']))
		echo    '<li><a  href="?mode=friendships'.$append_tw_id.'">Add To My Friend</a></li>';
		else
		echo    '<li><a class="selected" href="?mode=signout'.$append_tw_id.'">Sign out </a></li>';
	}
	else
	{
		echo    '<li><a class="selected" href="?mode=twitter'.$append_tw_id.'">Twitter</a></li>';
		echo     '<li><a href="?mode=friends'.$append_tw_id.'">Friends</a></li>';
		echo    '<li><a href="?mode=followers'.$append_tw_id.'">Followers</a></li>';
		if(isset($_REQUEST['id']))
		echo    '<li><a href="?mode=friendships'.$append_tw_id.'">Add To My Friend</a></li>';
		else
		echo    '<li><a href="?mode=signout">Sign out </a></li>';
	}
	echo    '</ul></div><hr />';

	echo '<div id="page" class="single">';
	echo '<div id="wrapper">';
	echo '<div id="content">';

	echo	'<ol class="commentlist">';

	echo	'<ol class="commentlist">';

	echo	'<ol class="commentlist">';

	if($select =='twitter')
	{
		if(isset($_REQUEST['id']))
		{	//you are lookup anther user
			$method = 'statuses/user_timeline';
			$parameters = array('id' => $id);
			//$parameters = array('count' => '20','screen_name'=>'htk_firefox');
		}
		else
		{	 //get your user info
			$method = 'statuses/home_timeline';
			$parameters = array();
		}
		$response = $connection->get($method, $parameters);
		foreach($response as $status){
			$user = $status ->user;
			echo	'<li class="comment " id="comment-200006">';
			echo   '<div class="commentHeader">';
			echo    '<span class="commentGrav">';
			echo    '<img height="25" width="25" class="gravatar" src="'.$user->profile_image_url.'" alt=""/>';
			echo	'</span>';
			echo 	'<span class="commentAuthor"><a href="http://www.twitter.com/'.$user->screen_name.'">'.$user->name.'</a></span>';
			echo	'<span class="commentDate"><a href="#comment-200006" title="">'.$status->created_at.'</a></span>';
			echo    '</div><div class="commentBody">';
			echo 	$status->text;
			echo	'</div></li></li>';
		}

	}
	else if ($select =='friends' ||$select =='followers')
	{	//for select for friends and followers;
		// statuses/friends
		// statuses/followers
		if(isset($_REQUEST['id']))
		{	//you are lookup  user id's infor
			$parameters = array('id' => $id);
		}
		else
		{	 //get your user info
			$parameters = array();
		}
		$method = 'statuses/'.$select;
		$response = $connection->get($method, $parameters);
		foreach($response as $user){
			$status = $user->status;
			echo	'<li class="comment " id="comment-200006">';
			echo   '<div class="commentHeader">';
			echo    '<span class="commentGrav">';
			echo    '<img height="25" width="25" class="gravatar" src="'.$user->profile_image_url.'" alt=""/>';
			echo	'</span>';
			echo 	'<span class="commentAuthor"><a href="http://www.twitter.com/'.$user->screen_name.'">'.$user->name.'</a></span>';
			echo	'<span class="commentDate"><a href="#comment-200006" title="">'.$status->created_at.'</a></span>';
			echo    '</div><div class="commentBody">';
			echo 	$status->text;
			echo	'</div></li></li>';
		}
	}
	else if ($select =='friendships')
	{	//for select for friendships
		// friendships/create
		// friendships/exists
		if(isset($_REQUEST['id']) && $_SESSION['twitter_id'] != $_REQUEST['id'])
		{	//is user id has been your friend ?
			$parameters = array('user_a' => $_SESSION['twitter_id'],'user_b'=>$id);
			$method = 'friendships/exists';
			$response = $connection->get($method, $parameters);
			/*  if (!is_string($response)) {
    			$response = print_r($response, TRUE);
  			} 
			echo 'return is :'.$response; */
			if (!$response)
			{  // we are not friend yet, create it now.
				$method = 'friendships/create';
				$parameters = array('id' => $id);
				$response = $connection->POST($method, $parameters);
			   /* if (!is_string($response)) {
    			$response = print_r($response, TRUE);
  				}
				echo $response;  */
				
				$user = $response;
				$status = $user->status;
				echo	'Hello my new friend !';
				echo	'<li class="comment " id="comment-200006">';
				echo   '<div class="commentHeader">';
				echo    '<span class="commentGrav">';
				echo    '<img height="25" width="25" class="gravatar" src="'.$user->profile_image_url.'" alt=""/>';
				echo	'</span>';
				echo 	'<span class="commentAuthor"><a href="http://www.twitter.com/'.$user->screen_name.'">'.$user->name.'</a></span>';
				echo	'<span class="commentDate"><a href="#comment-200006" title="">'.$status->created_at.'</a></span>';
				echo    '</div><div class="commentBody">';
				echo 	$status->text;
				echo	'</div></li></li>';
			}
			else
			{
				echo	'<li class="comment " id="comment-200006">';
				echo   '<div class="commentHeader">';
				echo    '<span class="commentGrav">';
				echo    '<img height="25" width="25" class="gravatar" src="'.$user->profile_image_url.'" alt=""/>';
				echo	'</span>';
				echo 	'<span class="commentAuthor"><a href="http://www.twitter.com/'.$user->screen_name.'">'.$user->name.'</a></span>';
				echo	'<span class="commentDate"><a href="#comment-200006" title="">'.$status->created_at.'</a></span>';
				echo    '</div><div class="commentBody">';
				echo 	'We have been friends...';
				echo	'</div></li></li>';

			}
		}
		else
		{	 //get your user info
			echo	'<li class="comment " id="comment-200006">';
			echo   '<div class="commentHeader">';
			echo    '<span class="commentGrav">';
			echo    '<img height="25" width="25" class="gravatar" src="'.$user->profile_image_url.'" alt=""/>';
			echo	'</span>';
			echo 	'<span class="commentAuthor"><a href="http://www.twitter.com/'.$user->screen_name.'">'.$user->name.'</a></span>';
			echo	'<span class="commentDate"><a href="#comment-200006" title="">'.$status->created_at.'</a></span>';
			echo    '</div><div class="commentBody">';
			echo 	'We have been known each other since brown...';
			echo 	'We have been friends...';
			echo	'</div></li></li>';
		}

	}
	else if ($select =='signout')
	{	
		if(isset($_REQUEST['confirmed']))
		{
			//for signout
			// account/end_session
			// friendships/exists
			$method = 'account/end_session';
			$response = $connection->POST($method);
			// clearnup
			unset($_SESSION['twitter_logged']);
			unset($_SESSION['twitter_name']);
			unset($_SESSION['twitter_logged_image']);
			unset($_SESSION['twitter_id']);
  
			unset($_SESSION['oauth_token']);
			unset($_SESSION['oauth_token_secret']);
			unset($_SESSION['access_token']);

			/* Redirect to page with the connect to Twitter option. */
			//header('Location: ./singout.php');
			
			/* clear sessions */
			//session_destroy();
			
			
			/* Start new session and load lib */
			//session_start();
			//require_once('JSON/JSON.php');
			//require_once('twitteroauth/twitteroauth.php');
			//require_once('config.php');
			
			
			/* Create TwitterOAuth object and get request token */
			$connection = new TwitterOAuth(CONSUMER_KEY, CONSUMER_SECRET);
			 
			/* Get request token */
			$request_token = $connection->getRequestTokenForceSignin(OAUTH_CALLBACK_TW,true);
			
			/* Save request token to session */
			$_SESSION['oauth_token'] = $token = $request_token['oauth_token'];
			$_SESSION['oauth_token_secret'] = $request_token['oauth_token_secret'];
			 
			/* If last connection fails don't display authorization link */
			switch ($connection->http_code) {
			  case 200:
			    /* Build authorize URL */
			    $url = $connection->getAuthorizeURL($token,null);
			    echo $url;
			    header('Location: ' . $url); 
			    //header('Location:index.php');
			    exit;
			  default:
			    echo 'Could not connect to Twitter. Refresh the page or try again later.';
			    break;
			}
						
		}
						
				
		echo	'You are currently sign-in twitter as user '.$user->name.'('.$user->screen_name.')';
		echo	'<li class="comment " id="comment-200006">';
		echo   '<div class="commentHeader">';
		echo    '<span class="commentGrav">';
		echo    '<img height="25" width="25" class="gravatar" src="'.$user->profile_image_url.'" alt=""/>';
		echo	'</span>';
		echo 	'<span class="commentAuthor"><a href="http://www.twitter.com/'.$user->screen_name.'">'.$user->name.'</a></span>';
		echo	'<span class="commentDate"><a href="#comment-200006" title="">'.$status->created_at.'</a></span>';
		echo    '</div><div class="commentBody">';
		echo 	'<a href="?mode=signout&confirmed">Sign out</a> if you want to connect to an account other than '.$user->screen_name;
		echo	'</div></li></li>';

	}
	//display a rate limet sattus at buttom.
	$content = $connection->get('account/rate_limit_status');
	echo "Current API hits remaining: {$content->remaining_hits}.";
	
	echo '</ol></div></div></div>';

}
?>

</body>
</html>
